<?php

App::uses('AppModel', 'Model');

class MountPrice extends AppModel {
    
    public $order = 'MountPrice.date DESC';
    
	public function calculate($date, $data) {
		$price     = 0;
		$mountPrice = (float)Set::extract('MountPrice.value', $this->find('first', array(
			'conditions' => array('MountPrice.date <=' => $date),
			'order'		 => 'MountPrice.date DESC'
		)));
		
		if (! empty($data['AdType'])) {
			foreach ($data['AdType'] as $adType) {
				$unit   = $adType['unit'];
				$width  = $adType['LocationsAdType']['width'];
				$height = $adType['LocationsAdType']['height'];
				
				$index = AdType::calculateIndex($unit, $width, $height);
				//debug("$price = $price + $mountPrice * $index");
				$price += $mountPrice * $index;
			}
		}
		
		return $price;
	}
}
